<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="region"/>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeView/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeView/bootstrap-treeview.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //克隆行
            var tr = $("tbody tr:first").clone();

            //页面加载完毕之后发送ajax请求,拿到所有的根区域
            $.get("/region/listMenus.do",{type:"treeView"},function (rootMenus) {
                //treeView数据结构特点:本身是一个数组,数组的每一个对象text属性,nodes属性
                $("#treeview").treeview({
                    color:"#428bca",
                    showTags:true,
                    data:[{text:"所有区域",nodes:rootMenus}],
                    lazyLoad:function (node) {
                        //node就是当前我点击的节点
                        //发送ajax请求,查询该区域的子区域
                        $.get("/region/listMenus.do",{parentId:node.id,type:"treeView"},function (childMenus) {
                            //把返回的节点数据添加到对应的父节点中
                            $('#treeview').treeview('addNode',[childMenus,node]);
                        });
                    },
                    //点击节点触发点击事件
                    onNodeSelected:function(ev,node) {
                        //显示表格
                        $("table").css("display","");
                        //发送ajax请求查询该区域的子区域
                        $.get("/region/listMenus.do",{parentId:node.id},function (childMenus) {
                            //先清空原来的数据,再渲染
                            $("tbody").html("");
                            //遍历所有的子节点
                            $.each(childMenus,function (index,ele) {
                                //复制行
                                var row = tr.clone();
                                //子节点第一个为索引,索引是从零开始,所以加1
                                row.find("td")[0].innerHTML = index + 1;
                                row.find("td")[1].innerHTML = ele.name;

                                //保存当前点击的行的数据
                                var json = {id:ele.id,name:ele.name,state:ele.state};
                                //发到所在行的修改的按钮中的参数去
                                row.find("a").attr("data-json",JSON.stringify(json));
                                //把行添加到tbody中
                                row.appendTo("tbody");
                            })
                        })
                    }
                })
            })

            //点击添加按钮时执行的操作
            $("#inputBtn").click(function () {
                //打开模态框
                showModal();
                //找到当前点击选中的节点
                var node = $('#treeview').treeview('getSelected')[0];
                //有的话则是在该节点下新增一个内容
                if(node){
                    $("#editForm input[name='parent.name']").val(node.text);
                    $("#editForm input[name='parent.id']").val(node.id);
                }else{
                    $("#editForm input[name='parent.name']").val("所有区域");
                }
            });

            //保存按钮,立刻提交
            $(".submitBtn").click(function () {
                $("#editForm").submit();
            });

            //在新建的每个节点里点击修改按钮,执行修改操作
            $("tbody").on("click",".inputBtn",function () {
                //获取按钮里面的json数据,用于回显
                var json = $(this).data("json");
                console.log(json);
                showModal(json);
            })
        })
        
        function showModal(json) {
            //先清空原来模态框里面的数据
            $("#inputModal input").val("");
            //如果有数据,代表修改,要回显数据
            if(json){
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm select[name='state']").val(json.state);
                var node = $('#treeview').treeview('getSelected')[0];
                if(node){
                    $("#editForm input[name='parent.name']").val(node.text);
                    $("#editForm input[name='parent.id']").val(node.id);
                }else{
                    $("#editForm input[name='parent.name']").val("所有区域");
                }
            }
            $("#inputModal").modal("show");
        }
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">旅游区域管理</h1>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-12">
                    <a href="javascript:" class="btn btn-success" id="inputBtn">
                        <span class="glyphicon glyphicon-plus"></span> 添加区域
                    </a>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-lg-8">
                    <table class="table table-striped table-hover" style="display: none">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>区域</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>xx</td>
                                <td>xx</td>
                                <td>
                                    <a href="javascript:" class="btn btn-info btn-xs inputBtn">
                                        <span class="glyphicon glyphicon-pencil"></span> 修改
                                    </a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal" method="post" action="/region/saveOrUpdate.do">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">区域名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入区域名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级区域</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parent.name" readonly/>
                            <input type="hidden" class="form-control" name="parent.id" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设为推荐</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:" class="btn btn-success submitBtn" aria-hidden="true">保存</a>
                <a href="javascript:" class="btn btn-info" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>
</body>
</html>